package com.example.myapplication3;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    private CheckBox cbAgree;
    private TextView tvPasswordHint;
    private TextView tvRegister;
    private ImageButton btnTogglePassword;

    private boolean isPasswordVisible = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        setupListeners();
    }

    private void initViews() {
        editTextUsername = findViewById(R.id.editText_username);
        editTextPassword = findViewById(R.id.editText_password);
        buttonLogin = findViewById(R.id.button_login);
        cbAgree = findViewById(R.id.cbAgree);
        tvPasswordHint = findViewById(R.id.tvPasswordHint);
        tvRegister = findViewById(R.id.tvRegister);
        btnTogglePassword = findViewById(R.id.btnTogglePassword);
    }

    private void setupListeners() {
        // 一、输入框基础交互
        // 1. 用户名输入框焦点变化
        editTextUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.setBackgroundResource(R.drawable.lx);
                } else {
                    v.setBackgroundResource(R.drawable.lx);
                    // 自动去掉前后空格
                    String text = editTextUsername.getText().toString().trim();
                    editTextUsername.setText(text);
                }
            }
        });

        // 2. 密码输入框焦点变化
        editTextPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editTextPassword.setBackgroundResource(R.drawable.lx);
                } else {
                    editTextPassword.setBackgroundResource(R.drawable.lx);
                }
            }
        });

        // 密码输入监听，显示/隐藏提示文字
        editTextPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    tvPasswordHint.setVisibility(View.VISIBLE);
                } else {
                    tvPasswordHint.setVisibility(View.GONE);
                }
            }
        });

        // 扩展功能：显示/隐藏密码
        btnTogglePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isPasswordVisible = !isPasswordVisible;
                if (isPasswordVisible) {
                    // 显示密码
                    editTextPassword.setInputType(android.text.InputType.TYPE_CLASS_TEXT);
                    btnTogglePassword.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
                } else {
                    // 隐藏密码
                    editTextPassword.setInputType(android.text.InputType.TYPE_CLASS_TEXT |
                            android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    btnTogglePassword.setImageResource(android.R.drawable.ic_menu_view);
                }
                // 移动光标到末尾
                editTextPassword.setSelection(editTextPassword.getText().length());
            }
        });

        // 二、协议勾选框
        cbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
            // 勾选后登录按钮变亮，未勾选时灰色
            buttonLogin.setEnabled(isChecked);
            buttonLogin.setBackgroundTintList(getResources().getColorStateList(
                    isChecked ? android.R.color.holo_blue_light : android.R.color.darker_gray));
        });

        // 三、登录按钮
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = editTextUsername.getText().toString().trim();
                String password = editTextPassword.getText().toString().trim();

                // 检查输入
                if (username.isEmpty()) {
                    Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!cbAgree.isChecked()) {
                    Toast.makeText(MainActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 都填好且勾选了协议 → 登录成功
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });

        // 四、注册提示文字
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "跳转到注册页", Toast.LENGTH_SHORT).show();
            }
        });
    }
}